package com.zoe.onelink.core.encryptor.algorithm;

import com.zoe.onelink.annotation.data.encryptor.IEncryptor;
import com.zoe.onelink.common.exception.ApiException;
import com.zoe.onelink.core.property.OnelinkProperties;

/**
 * <p>标题: </p>
 * <p>描述: </p>
 * <p>版权: Copyright (c) 2021</p>
 * <p>公司: 智业软件股份有限公司</p>
 *
 * @version: 1.0
 * @author: ruansheng
 * @date: 2021-11-12
 */
public abstract class AbstractEncryptFieldAlgorithm implements IEncryptor {

    private final OnelinkProperties.Web.Encrypt encryptProp;

    /**
     * 如果在创建算法的时候直接在构造函数里面抛出,{@link org.springframework.web.servlet.mvc.method.annotation.AbstractMessageConverterMethodProcessor}
     * 中的处理方式是直接抛出HttpMessageNotWritableException异常,会将自定义异常屏蔽
     */
    protected ApiException ex;

    protected AbstractEncryptFieldAlgorithm(OnelinkProperties.Web.Encrypt encryptProp) {
        this.encryptProp = encryptProp;
    }

    @Override
    public String encrypt(String text) {

        if (this.ex != null) {
            throw ex;
        }

        return this.doEncrypt(text);
    }

    @Override
    public String decrypt(String text) {

        if (this.ex != null) {
            throw ex;
        }

        return this.doDecrypt(text);
    }

    /**
     * 加密文本
     */
    protected abstract String doEncrypt(String text);

    /**
     * 解密文本
     */
    protected abstract String doDecrypt(String text);

}
